package cn.javabb.admin.controller.sys;

import cn.javabb.common.model.ResponseModel;
import cn.javabb.common.utils.ListUtils;
import cn.javabb.common.utils.ResultUtil;
import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.sys.ConfigDO;
import cn.javabb.core.service.sys.SysConfigService;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @Author QINB imqinbao@163.com
 * @CreateDate 2018/11/22/022 15:58
 * @Since V1.0
 */
@Controller
@RequestMapping("/admin/config")
public class SysConfigController extends BaseController {

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * @author QINB
     * @param model
     * @return
     */
    @RequiresPermissions("config:view")
    @GetMapping("/list")
    public String toListPage(Model model) {
        return "admin/system/config";
    }
    /**
     * @author QINB
     * @param model
     * @return
     */
    @RequiresPermissions("config:view")
    @GetMapping("/list2")
    public String toListPage2(Model model) {
        return "admin/system/config2";
    }
    /**
     * @author QINB
     * @param model
     * @return
     */
    @GetMapping("/save")
    public String toSavePage(Model model) {
        return "admin/system/configSave";
    }

    /**
     * 系统配置
     * @param model
     * @return
     */
    @GetMapping("/sysConfig")
    public ModelAndView sysConfig(Model model) {
        return ResultUtil.view("admin/config/sysConfig");
    }

    /**
     * 内容配置
     * @param model
     * @return
     */
    @GetMapping("/bizConfig")
    public ModelAndView bizConfig(Model model) {
        return ResultUtil.view("admin/config/bizConfig");
    }
    /**
     * 上传配置
     * @param model
     * @return
     */
    @GetMapping("/uploadConfig")
    public ModelAndView uploadConfig(Model model) {
        return ResultUtil.view("admin/config/uploadConfig");
    }
    /**
     * 上传配置
     * @param model
     * @return
     */
    @GetMapping("/adConfig")
    public ModelAndView adConfig(Model model) {
        return ResultUtil.view("admin/config/adConfig");
    }
    /**
     * 邮件配置
     * @param model
     * @return
     */
    @GetMapping("/emailConfig")
    public ModelAndView emailConfig(Model model) {
        return ResultUtil.view("admin/config/emailConfig");
    }
    /**
     * 查询角色列表
     */
    @RequiresPermissions("config:view")
    @ResponseBody
    @GetMapping("/")
    public Object list(String configCode) {

        String page = getPage();
        String size = getSize();
        ConfigDO sc = new ConfigDO();
        if (StringUtils.isNotBlank(configCode)) {
            sc.setConfigCode(configCode);
        }
        Page configPage = sysConfigService.queryPageByWhere(page, size, sc);
        return ResponseModel.table(configPage);
    }
    /**
     * 获取配置
     */
    @RequiresPermissions("config:view")
    @ResponseBody
    @GetMapping("/getConfig")
    public Object getConfig(String configType) {
        Map<String,String> configMap = sysConfigService.getConfigs(configType);
        return ResponseModel.Success(configMap);
    }
    @RequiresPermissions("config:save")
    @ResponseBody
    @PostMapping("/")
    public Object saveOrUpdate(ConfigDO config){
        if(sysConfigService.saveOrUpdate(config)){
            return ResponseModel.Success("保存成功");
        }
        return ResponseModel.Failure("保存失败");
    }

    @RequiresPermissions("config:save")
    @ResponseBody
    @PostMapping("/saveMap")
    public Object saveMap(@RequestParam Map<String,String> map){
        if(sysConfigService.batchUpdate(map)){
            return ResponseModel.Success("保存成功");
        }
        return ResponseModel.Failure("保存失败");
    }

    @RequiresPermissions("config:del")
    @ResponseBody
    @DeleteMapping("/{configId}")
    public Object delete(@PathVariable String configId){
        if(StringUtils.isBlank(configId)){
            return ResponseModel.Failure("参数错误");
        }
        List<String> idList = ListUtils.stringToListString(configId);
        int num = sysConfigService.deleteByIds(ConfigDO.class, "configId", idList);
        if(num>0){
            return ResponseModel.Success("删除成功");
        }
        return ResponseModel.Failure("删除失败");
    }
}
